package com.meirengu.webview.service.impl;

import com.meirengu.utils.DateUtils;
import com.meirengu.webview.model.*;
import com.meirengu.webview.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private KwOperationLogService kwOperationLogService;
    @Autowired
    CheckCodeService checkCodeService;
    @Autowired
    private KwChannelService kwChannelService;
    @Autowired
    private KwChannelDataService kwChannelDataService;
    @Autowired
    private KwProductService kwProductService;
    @Autowired
    private KwTrackEventService kwTrackEventService;
    @Autowired
    private KwInterfaceConfigService kwInterfaceConfigService;
    @Autowired
    private KwInterfaceLimitService kwInterfaceLimitService;
    @Autowired
    private KwAreaLimitService kwAreaLimitService;
    @Autowired
    private KwAreaDataService kwAreaDataService;

    @Override
    public synchronized void scheduledAddKwChannel(String jobDate) throws Exception {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("status", 1);
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(jobDate)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(sdf.parse(jobDate));
        }
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date logTimeBegin = DateUtils.getDayBeginTime(calendar.getTime());
        Date logTimeEnd = DateUtils.getDayEndTime(calendar.getTime());

        List<KwChannel> chList = kwChannelService.getModel(paramMap);
        kwOperationLogService.addOperationLog("2", "定时增加卡王贷渠道统计数据", "addKwChannel",
                "定时任务", DateUtils.dateToStr(logTimeBegin), DateUtils.dateToStr(logTimeEnd), "kw_channel_data", 0,
                null, null, null);
        if (chList.size() > 0) {
            for (KwChannel kwChannel : chList) {
                KwChannelData kwChannelData = new KwChannelData();
                kwChannelData.setChannelCode(kwChannel.getChannelCode());
                kwChannelData.setChannelName(kwChannel.getChannelName());
                kwChannelData.setRegCutRate(kwChannel.getRegCutRate());
                kwChannelData.setRegCutRateBase(kwChannel.getRegCutRateBase());
                kwChannelData.setApplyCutRate(kwChannel.getApplyCutRate());
                kwChannelData.setApplyCutRateBase(kwChannel.getApplyCutRateBase());
                kwChannelData.setCreateName(kwChannel.getStaffAccount());
                kwChannelData.setCreateDate(new Date());
                kwChannelData.setChannelId(kwChannel.getId());
                kwChannelData.setVisitNum(0);
                kwChannelData.setRegistNum(0);
                kwChannelData.setApplyNum(0);
                kwChannelData.setO2ONum(0);
                kwChannelData.setProductNum(0);
                kwChannelData.setDataDate(calendar.getTime());
                kwChannelDataService.insert(kwChannelData);
            }
        }

    }

    @Override
    public synchronized void scheduledAddKwTrackEvent() throws Exception {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("status", 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date logTimeBegin = DateUtils.getDayBeginTime(calendar.getTime());
        Date logTimeEnd = DateUtils.getDayEndTime(calendar.getTime());

        List<KwProduct> productList = kwProductService.getModel(paramMap);
        kwOperationLogService.addOperationLog("2", "定时增加卡王贷产品点击统计", "addKwTrackEvent",
                "定时任务", DateUtils.dateToStr(logTimeBegin), DateUtils.dateToStr(logTimeEnd), "add_kw_trackEvent", 0,
                null, null, null);
        if (productList.size() > 0) {
            for (KwProduct kwProduct : productList) {
                KwTrackEvent kwTrackEvent = new KwTrackEvent();
                kwTrackEvent.setCreateName("定时任务");
                kwTrackEvent.setCreateBy("job");
                kwTrackEvent.setCreateDate(new Date());
                kwTrackEvent.setCategory(kwProduct.getProductName());
                kwTrackEvent.setAction("跳转");
                kwTrackEvent.setLabel(kwProduct.getId());
                kwTrackEvent.setPv(0);
                kwTrackEvent.setUv(0);
                kwTrackEvent.setNewuv(0);
                kwTrackEvent.setDataDate(calendar.getTime());
                kwTrackEventService.insert(kwTrackEvent);
            }
        }
    }

    @Override
    public synchronized void scheduledAddLimit(String jobDate) throws Exception {

        Map<String, Object> statusMap = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(jobDate)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(sdf.parse(jobDate));
        }
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date logTimeBegin = DateUtils.getDayBeginTime(calendar.getTime());
        Date logTimeEnd = DateUtils.getDayEndTime(calendar.getTime());

        List<KwInterfaceConfig> configList = kwInterfaceConfigService.getModel(statusMap);
        kwOperationLogService.addOperationLog("2", "定时增加推送数据统计", "addLimit",
                "定时任务", DateUtils.dateToStr(logTimeBegin), DateUtils.dateToStr(logTimeEnd), "kw_interface_limit", 0,
                null, null, null);
        if (configList.size() > 0) {
            for (KwInterfaceConfig kwInterfaceConfig : configList) {
                KwInterfaceLimit kwInterfaceLimit = new KwInterfaceLimit();
                kwInterfaceLimit.setInterfaceCode(kwInterfaceConfig.getInterfaceCode());
                kwInterfaceLimit.setInterfaceName(kwInterfaceConfig.getInterfaceName());
                kwInterfaceLimit.setLimitNum(kwInterfaceConfig.getLimitNum());
                kwInterfaceLimit.setNum(kwInterfaceConfig.getNum());
                kwInterfaceLimit.setSendNum(0);
                kwInterfaceLimit.setOutLimit("N");
                kwInterfaceLimit.setDataDate(calendar.getTime());
                kwInterfaceLimit.setCreateDate(new Date());
                kwInterfaceLimitService.insert(kwInterfaceLimit);
            }
        }

    }

    @Override
    public synchronized void scheduledAddAreaLimitData() throws Exception {
        Map<String, Object> statusMap = new HashMap<>();
        statusMap.put("areaLimit", "Y");
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date logTimeBegin = DateUtils.getDayBeginTime(calendar.getTime());
        Date logTimeEnd = DateUtils.getDayEndTime(calendar.getTime());
        List<KwAreaLimit> areaList = kwAreaLimitService.getModel(statusMap);
        kwOperationLogService.addOperationLog("2", "定时增加地区限制数量统数据", "addAreaData",
                "定时任务", DateUtils.dateToStr(logTimeBegin), DateUtils.dateToStr(logTimeEnd), "kw_area_data", 0,
                null, null, null);
        if (areaList.size() > 0) {
            for (KwAreaLimit kwAreaLimit : areaList) {
                KwAreaData kwAreaData = new KwAreaData();
                kwAreaData.setCreateDate(new Date());
                kwAreaData.setChannelCode(kwAreaLimit.getChannelCode());
                kwAreaData.setChannelName(kwAreaLimit.getChannelName());
                kwAreaData.setAreaCode(kwAreaLimit.getAreaCode());
                kwAreaData.setAreaName(kwAreaLimit.getAreaName());
                kwAreaData.setNum(kwAreaLimit.getNum());
                kwAreaData.setPushNum(0);
                kwAreaData.setDataDate(calendar.getTime());
                kwAreaData.setOutLimit("N");
                kwAreaDataService.insert(kwAreaData);
            }
        }

    }
}
